﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PropertyManagement.Core.DataAccess;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Modal {
    public partial class UnitPriceEditor : Form {
        private FeeUnitPriceManage feeUnitPrice;
        public UnitPriceEditor( ) {
            InitializeComponent( );
        }

        public UnitPriceEditor(BillType billType)
            : this( ) {
            feeUnitPrice = FeeUnitPriceManageDataAccess.GetUnitByBillType(billType);
            if (feeUnitPrice == null) {
                this.Close();
            }
            else {
                label1.Text = "修改" + BillTypeHelper.GetBillTypeName(billType) + "单价";
                textBox1.Text = feeUnitPrice.UnitPrice.ToString("#0.00");
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            var newUnitPrice = 0m;
            if (decimal.TryParse(textBox1.Text, out newUnitPrice)) {
                if (checkBox1.Checked) {
                    feeUnitPrice.UnitPrice = newUnitPrice;
                    feeUnitPrice.LastUpdate = DateTime.Now;
                    feeUnitPrice.LastActionUserID = GlobalSetting.CurrentSystemUser.SystemUserID;
                    FeeUnitPriceManageDataAccess.Update(feeUnitPrice);
                }
                else {
                    feeUnitPrice.IsExpire = true;
                    feeUnitPrice.LastUpdate = DateTime.Now;
                    feeUnitPrice.LastActionUserID = GlobalSetting.CurrentSystemUser.SystemUserID;
                    FeeUnitPriceManageDataAccess.Update(feeUnitPrice);

                    FeeUnitPriceManageDataAccess.Add(new FeeUnitPriceManage {
                        BillType = feeUnitPrice.BillType,
                        UnitPrice = newUnitPrice,
                        IsExpire = false,
                        CreationDate = DateTime.Now,
                        CreationUserID = GlobalSetting.CurrentSystemUser.SystemUserID,
                        LastUpdate = DateTime.Now,
                        LastActionUserID = GlobalSetting.CurrentSystemUser.SystemUserID
                    });
                }
                FormHelper.ShowInfoMsg("修改单价成功");
                this.Close();
            }
            else {
                FormHelper.ShowErrorMsg("输入的单价格式错误");
            }
            
        }

        private void button2_Click(object sender, EventArgs e) {
            this.Close( );
        }
    }
}
